﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System8Equations;

namespace System8ToyMC
{
	public partial class MainPage : UserControl
	{
        System8Equations.S8Equations _eqns = new System8Equations.S8Equations();

        public DataLoadSaveHelper SavedResults
        {
            get { return (DataLoadSaveHelper)GetValue(SavedResultsProperty); }
            set { SetValue(SavedResultsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SavedResults.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SavedResultsProperty =
            DependencyProperty.Register("SavedResults", typeof(DataLoadSaveHelper), typeof(MainPage), new PropertyMetadata(null));

        //public DataLoadSaveHelper SavedResults {get; private set;}

		public MainPage()
		{
            SavedResults = new DataLoadSaveHelper();
            
            // Required to initialize variables
			InitializeComponent();
            LayoutRoot.DataContext = this;

            System8Params.Equations = _eqns;

            SaveLoad.LoadSavedData += new Action<string>(LoadSavedData);
            SaveLoad.SaveData += new Action<string>(SaveCurrentData);

            ConversionTextEntry.Convert += new Action<object, EventArgs>(ConversionTextEntry_Convert);

            ///
            /// Make sure any plots we have going are correctly updated!
            /// 

            //_eqns.ValuesUpdated += new Action(() => Dispatcher.BeginInvoke(() => PlotAreaManager.Calculate(_eqns)));
            _eqns.ValuesUpdated += new Action(() => System.Threading.ThreadPool.QueueUserWorkItem(o => PlotAreaManager.Calculate(_eqns)));
        }

        /// <summary>
        /// Saved the current data set!
        /// </summary>
        /// <param name="obj"></param>
		private void SaveCurrentData(string dsName)
		{
            SavedResults.SaveCurrentState(dsName, _eqns);
		}

        /// <summary>
        /// Load the current data
        /// </summary>
        /// <param name="obj"></param>
        private void LoadSavedData(string dsName)
		{
            PlotAreaManager.BeginBatchUpdate();
            SavedResults.LoadCurrentState(dsName, _eqns);
            PlotAreaManager.EndBatchUpdate();
		}

        /// <summary>
        /// We have somethign good from the conversion - do it!
        /// </summary>
        private void ConversionTextEntry_Convert(object source, EventArgs args)
        {
            var sourceText = ConversionTextEntry.Text;

            var converter = new S8TextParser();
            PlotAreaManager.BeginBatchUpdate();
            converter.Parse(System8Params.Equations, sourceText);
            PlotAreaManager.EndBatchUpdate();
        }

	}
}